﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using WeiboPlus.Common;
using WeiboPlus.Shell.ViewModel;

namespace WeiboPlus.Shell.Views
{
    public partial class SettingView : PhoneApplicationPage
    {
        SettingViewModel _vm;
        public SettingView()
        {
            InitializeComponent();
            DataContext = _vm = new SettingViewModel();

            this.Loaded += (o, e) =>
            {
                var back = new ApplicationBarIconButton(new Uri("/Images/AppBarIcons/appbar.back.rest.png", UriKind.Relative)) { Text = AppResx.GoBackText };
                back.Click += new EventHandler(back_Click);
                ApplicationBar.Buttons.Add(back);

                var aib1 = new ApplicationBarIconButton
                {
                    IconUri = new Uri("/Images/AppBarIcons/appbar.check.rest.png", UriKind.Relative),
                    Text = AppResx.SaveText,
                };

                aib1.Click += ok_Click;

                var aib2 = new ApplicationBarIconButton
                {
                    IconUri = new Uri("/Images/AppBarIcons/refresh.png", UriKind.Relative),
                    Text = AppResx.ResetText,
                };

                aib2.Click += reset_Click;

                this.ApplicationBar.Buttons.Add(aib1);
                this.ApplicationBar.Buttons.Add(aib2);
            };
        }

        private void reset_Click(object sender, EventArgs e)
        {
            _vm.ResetSetting();
        }

        private void ok_Click(object sender, EventArgs e)
        {
            PhoneApplicationController.ApplyLanguageSetting();
            PhoneApplicationController.ApplyTheme();

            NavigationService.Navigate(new Uri("/Views/MainView.xaml", UriKind.Relative));
        }

        private void CheckBox_UnChecked(object sender, RoutedEventArgs e)
        {
            //if (cbOrientation.IsChecked.Value)
            //{
            //    _vm.Item.CurrentSupportedOrientation = (int)SupportedPageOrientation.PortraitOrLandscape;
            //}
            //else
            //{
            if ((this.Orientation & PageOrientation.Landscape) == PageOrientation.Landscape)
            {
                SupportedOrientations = SupportedPageOrientation.Landscape;
            }
            else if ((this.Orientation & PageOrientation.Portrait) == PageOrientation.Portrait)
            {
                SupportedOrientations = SupportedPageOrientation.Portrait;
            }
            _vm.Item.CurrentOrientation = (int)this.Orientation;
            _vm.Item.CurrentSupportedOrientation = (int)SupportedOrientations;

        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
        //    if (cbOrientation.IsChecked.Value)
        //    {
                _vm.Item.CurrentSupportedOrientation = (int)SupportedPageOrientation.PortraitOrLandscape;
            //}
            //else
            //{
            //    if ((this.Orientation & PageOrientation.Landscape) == PageOrientation.Landscape)
            //    {
            //        SupportedOrientations = SupportedPageOrientation.Landscape;
            //    }
            //    else if ((this.Orientation & PageOrientation.Portrait) == PageOrientation.Portrait)
            //    {
            //        SupportedOrientations = SupportedPageOrientation.Portrait;
            //    }
            //    _vm.Item.CurrentOrientation = (int)this.Orientation;
            //    _vm.Item.CurrentSupportedOrientation = (int)SupportedOrientations;
            //}

        }

        protected override void OnOrientationChanged(OrientationChangedEventArgs e)
        {
            base.OnOrientationChanged(e);

            switch (e.Orientation)
            {
                case PageOrientation.LandscapeLeft:
                    pivot.SetValue(FrameworkElement.MarginProperty, new Thickness(0, 0, 70, 0));
                    break;
                case PageOrientation.LandscapeRight:
                    pivot.SetValue(FrameworkElement.MarginProperty, new Thickness(70, 0, 0, 0));
                    break;
                case PageOrientation.PortraitDown:
                    pivot.SetValue(FrameworkElement.MarginProperty, new Thickness(0, 0, 0, 70));
                    break;
                case PageOrientation.PortraitUp:
                    pivot.SetValue(FrameworkElement.MarginProperty, new Thickness(0, 0, 0, 70));
                    break;
                default:
                    break;

            }

        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            if (PhoneApplicationController.Current.AppSetting.EnableOrientationChange)
            {
                SupportedOrientations = SupportedPageOrientation.PortraitOrLandscape;
            }
            else
            {
                var orientation = (SupportedPageOrientation)PhoneApplicationController.Current.AppSetting.CurrentSupportedOrientation;
                this.SupportedOrientations = orientation;
                this.Orientation = (PageOrientation)PhoneApplicationController.Current.AppSetting.CurrentOrientation;
            }
        }

        void back_Click(object sender, EventArgs e)
        {
            if (NavigationService.CanGoBack)
                NavigationService.GoBack();
            //throw new NotImplementedException();
        }
    }
}